/*
 * FileName：ReplyKeyController.java 
 * <p>
 * Copyright (c) 2017-2020 <a href="https://www.smartwx.info">hermit(1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.wx.wxcms.ctrl;

import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.smart.common.BaseCtrl;
import com.smart.common.util.AjaxResult;
import com.smart.common.util.SessionUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.smart.wx.wxcms.entity.MediaFiles;
import com.smart.wx.wxcms.entity.MsgNews;
import com.smart.wx.wxcms.entity.ReplyKey;
import com.smart.wx.wxcms.entity.ReplyKeyword;
import com.smart.wx.wxcms.service.MediaFileService;
import com.smart.wx.wxcms.service.MsgNewsService;
import com.smart.wx.wxcms.service.ReplyKeyService;
import com.smart.wx.wxcms.service.ReplyKeywordService;

/**
 * 
 *
 * @author hermit
 * @version 2.0
 * @date 2018-07-25 09:40:03
 */
@Controller
@RequestMapping("/wxcms/replykey")
public class ReplyKeyController extends BaseCtrl {
    @Resource
    private ReplyKeyService replyKeyService;
    @Resource
    private ReplyKeywordService replyKeywordService;
    @Autowired
    private MsgNewsService msgNewsService;
	@Autowired
	private MediaFileService mediaFileService;
    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public AjaxResult list(ReplyKey replyKey) throws Exception {
        List<ReplyKey> replyKeyList = replyKeyService.queryListByPage(replyKey);
        return getResult(replyKey, replyKeyList);
    }

    /**
     * 信息
     */
    @ResponseBody
    @RequestMapping("/info")
    public AjaxResult info(ReplyKey replyKey) throws Exception {
		ReplyKey one =replyKeyService.queryObject(replyKey);
		JSONObject json=(JSONObject)JSON.toJSON(one);
		ReplyKeyword keyWord=new ReplyKeyword();
		keyWord.setKId(one.getId());
		List<ReplyKeyword> keyList = replyKeywordService.queryList(keyWord);
		if(keyList!=null && !keyList.isEmpty()) {
			json.put("keyWords", keyList);
		}
		if(one.getMsgType().equals("news")){
			List<MsgNews> listNews = msgNewsService.getByMediaId(one.getMediaId());
			//判断数据库是否存在数据
			if(listNews!=null&&!listNews.isEmpty()){
				json.put("data", listNews.get(0));
			}
		}else{
			if(!one.getMsgType().equals("text")){
    			
    			MediaFiles media = mediaFileService.getFileByMediaId(one.getMediaId());
    			json.put("data", media);
    		}
		}
        return AjaxResult.success(json);
    }

    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping("/save")
    public AjaxResult save(ReplyKey replyKey,@RequestParam("keys[]") String[] keys) throws Exception {
    	replyKey.setUserId(SessionUtil.getUserId());
			replyKeyService.insert(replyKey);
			for (int i = 0; i < keys.length; i++) {
				ReplyKeyword keyWord=new ReplyKeyword();
				keyWord.setUserId(SessionUtil.getUserId());
				keyWord.setKId(replyKey.getId());
				keyWord.setKey(keys[i]);
				replyKeywordService.insert(keyWord);
			}
			
        return AjaxResult.saveSuccess();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    public AjaxResult update(ReplyKey replyKey ,@RequestParam("keys[]") String[] keys) throws Exception {
    	replyKey.setUserId(SessionUtil.getUserId());
			replyKeyService.update(replyKey);
			//这里先删除后添加
			replyKeywordService.deleteByKid(replyKey.getId());
			
			for (int i = 0; i < keys.length; i++) {
				ReplyKeyword keyWord=new ReplyKeyword();
				keyWord.setUserId(SessionUtil.getUserId());
				keyWord.setKId(replyKey.getId());
				keyWord.setKey(keys[i]);
				replyKeywordService.insert(keyWord);
			}
        return AjaxResult.updateSuccess();
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    public AjaxResult delete(ReplyKey replyKey) throws Exception {
    	replyKeyService.delete(replyKey);
        return AjaxResult.deleteSuccess();
    }

    /**
     * 批量删除
     */
    @ResponseBody
    @RequestMapping("/deleteBatch")
    public AjaxResult deleteBatch(@RequestParam("ids[]") String[] ids) throws Exception {
    	replyKeyService.deleteBatch(Arrays.asList(ids));
        return AjaxResult.deleteSuccess();
    }
}
